package com.qd.panda.hk;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cdqidi.dto.ApiResult;
import com.cdqidi.dto.ApiResultWrapper;
import com.cdqidi.dto.DelDTO;
import com.cdqidi.util.page.PageUtil;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.qd.common.panda.domain.entity.hk.HkParkOrderDTO;
import com.qd.common.sys.annotation.OperatorInfo;
import com.qd.common.sys.enums.OperatorType;
import com.qd.panda.service.hk.HkParkOrderService;
import com.qd.panda.service.hk.HkService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import java.util.List;

/**
 * WEB_临停订单
 *
 * @author sjk
 */
@RequiredArgsConstructor
@Validated
@RestController
@RequestMapping("/sys/hkParkOrder/")
@Slf4j
public class HkParkOrderController {
    private final HkParkOrderService hkParkOrderService;
    private final HkService hkService;

    /**
     * 删除
     *
     * @param delDTO delDTO中的ids,主键
     * @return 成功 or 失败
     */
    @PreAuthorize("@ps.hasPermission('system:hkParkOrder:delete')")
    @OperatorInfo(title = "删除", operator = OperatorType.DELETE)
    @DeleteMapping(value = "/delete")
    public ApiResultWrapper<Object> delete(@Validated @RequestBody DelDTO<String> delDTO) {
        List<String> ids = delDTO.getIds();
        if (ids.size() == 1) {
            hkParkOrderService.removeById(ids.get(0));
        } else {
            hkParkOrderService.removeBatchIds(ids);
        }
        return ApiResult.success("删除成功");
    }

    /**
     * 详情
     *
     * @param id 主键
     * @return 记录
     */
    @GetMapping(value = "/getById")
    public HkParkOrderDTO getById(@Validated @NotBlank(message = "ID不能为空") String id) {
        return hkParkOrderService.getByIdDto(id);
    }

    /**
     * 列表
     *
     * @param dto 查询参数以及页数和每页显示条数
     * @return 列表信息
     */
    @GetMapping(value = "/page")
    public IPage<HkParkOrderDTO> page(HkParkOrderDTO dto) {
        return hkParkOrderService.page(PageUtil.buildPage(dto), dto);
    }

    /**
     * 同步到海康
     *
     * @param dto 查询参数以及页数和每页显示条数
     * @return 列表信息
     */
    @PreAuthorize("@ps.hasPermission('system:hkParkOrder:syncToHk')")
    @GetMapping(value = "/syncToHk")
    public ApiResultWrapper<Object> syncToHk(HkParkOrderDTO dto) {
        hkService.syncToHk(dto);
        return ApiResult.success();
    }

    /**
     * 导出
     *
     * @param query 查询参数
     */
    @PreAuthorize("@ps.hasPermission('system:hkParkOrder:export')")
    @OperatorInfo(title = "导出", operator = OperatorType.EXPORT)
    @GetMapping(value = "/export")
    public void export(HkParkOrderDTO query) {
        hkParkOrderService.export(query);
    }

    /**
     * 统计
     *
     * @param query 查询参数
     * @return 统计
     */
    @PreAuthorize("@ps.hasPermission('system:hkParkOrder:statistics')")
    @OperatorInfo(title = "统计", operator = OperatorType.OTHER)
    @GetMapping(value = "/statistics")
    public ArrayNode statistics(HkParkOrderDTO query) {
        return hkParkOrderService.statisticsHkOrder(query);
    }
}